#include "RAT_ResourceManager.h"

namespace RAT_ENGINE
{
		RAT_ResourceManager::RAT_ResourceManager()
		{
		}
		RAT_ResourceManager::~RAT_ResourceManager()
		{
		}
		
		void RAT_ResourceManager::Init()
		{
		}

		void RAT_ResourceManager::Init(std::string argResourceFileURL)
		{
			std::ifstream resourceFile;
			resourceFile.open(argResourceFileURL);

			while (resourceFile.good())
			{
				std::string currentLine;
				std::string type;
				std::string url;

				std::getline(resourceFile, currentLine);

				type = currentLine.substr(0, currentLine.find(";"));
				url = currentLine.substr(currentLine.find(";") + 1);

				if (type == "custommesh")
				{
					LoadCustomMesh(url);
				}
				else
				{
					std::cout << "Failed to load: " << currentLine << std::endl;
				}
			}

			resourceFile.close();
		}

		void RAT_ResourceManager::CleanUp()
		{
		}

		void RAT_ResourceManager::AddCustomMesh( RAT_CustomMesh* argMesh)
		{
			customMeshes.push_back(argMesh);
		}

		void RAT_ResourceManager::LoadCustomMesh(const std::string argMeshFileURL)
		{
			std::ifstream meshFile;
			meshFile.open(argMeshFileURL);

			RAT_CustomMesh* newMesh;
			newMesh = new RAT_CustomMesh();
			newMesh->Init(customMeshes.size(), argMeshFileURL);

			while (meshFile.good())
			{
				std::stringstream ssx;
				std::stringstream ssy;
				std::stringstream ssz;
				std::stringstream ssrhw;
				
				std::string currentLine;

				std::getline(meshFile, currentLine);

				RAT_CustomVertex vertex;

				std::string x, y, z; 
				std::string rhw;
				std::string colour;

				x = currentLine.substr(0, currentLine.find(";"));
				currentLine = currentLine.substr(currentLine.find(";") + 1);

				y = currentLine.substr(0, currentLine.find(";"));
				currentLine = currentLine.substr(currentLine.find(";") + 1);

				z = currentLine.substr(0, currentLine.find(";"));
				currentLine = currentLine.substr(currentLine.find(";") + 1);

				rhw = currentLine.substr(0, currentLine.find(";"));
				currentLine = currentLine.substr(currentLine.find(";") + 1);

				colour = currentLine.substr(0, currentLine.find(";"));
				currentLine = currentLine.substr(currentLine.find(";") + 1);

				ssx << x;
				ssx >> vertex.position.x;

				ssy << y;
				ssy >> vertex.position.y;

				ssz << z;
				ssz >> vertex.position.z;

				ssrhw << rhw;
				ssrhw >> vertex.rhw;


				if (colour == "red")
				{
					vertex.colour = RED;
				}
				else if (colour == "black")
				{
					vertex.colour = BLACK;
				}
				else if (colour == "blue")
				{
					vertex.colour = BLUE;
				}
				else if (colour == "brown")
				{
					vertex.colour = BROWN;
				}
				else if (colour == "green")
				{
					vertex.colour = GREEN;
				}
				else if (colour == "grey")
				{
					vertex.colour = GREY;
				}
				else if (colour == "orange")
				{
					vertex.colour = ORANGE;
				}
				else if (colour == "purple")
				{
					vertex.colour = PURPLE;
				}
				else if (colour == "white")
				{
					vertex.colour = WHITE;
				}
				else if (colour == "yellow")
				{
					vertex.colour = YELLOW;
				}

				newMesh->AddVertex(vertex);
			}

			AddCustomMesh(newMesh);

			meshFile.close();
		}

		RAT_CustomMesh* RAT_ResourceManager::GetCustomMesh(const int argID) const
		{
			return customMeshes[argID];
		}

		RAT_CustomMesh* RAT_ResourceManager::GetCustomMesh(const std::string argMeshFileURL) const
		{
			unsigned int index = 0;
			while (index < customMeshes.size() && customMeshes[index]->GetURL() != argMeshFileURL)
			{
				++index;
			}

			return customMeshes[index];
		}
		

		std::vector<RAT_CustomMesh*> RAT_ResourceManager::GetCustomMeshes() const
		{
			return customMeshes;
		}

		void RAT_ResourceManager::PrintIO()
		{
			std::cout << "Resources:" << std::endl;
			std::cout << "CustomMeshes:" << std::endl;
			for (unsigned int i = 0; i < customMeshes.size(); ++i)
			{
				customMeshes[i]->PrintIO();
				std::cout << std::endl;
			}
		}
}
